home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / map-ynp.el < prev    next >
Lisp/Scheme  |  1993-07-04  |  8KB  |  223 lines

  1. ;;; map-ynp.el --- General-purpose boolean question-asker.
  2.  
  3. ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
  6. ;; Keywords: lisp, extensions
  7.  
  8. ;;; This program is free software; you can redistribute it and/or modify
  9. ;;; it under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 2, or (at your option)
  11. ;;; any later version.
  12. ;;;
  13. ;;; This program is distributed in the hope that it will be useful,
  14. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; A copy of the GNU General Public License can be obtained from this
  19. ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
  20. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  21. ;;; 02139, USA.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;;; map-y-or-n-p is a general-purpose question-asking function.
  26. ;;; It asks a series of y/n questions (a la y-or-n-p), and decides to
  27. ;;; applies an action to each element of a list based on the answer.
  28. ;;; The nice thing is that you also get some other possible answers
  29. ;;; to use, reminiscent of query-replace: ! to answer y to all remaining
  30. ;;; questions; ESC or q to answer n to all remaining questions; . to answer
  31. ;;; y once and then n for the remainder; and you can get help with C-h.
  32.  
  33. ;;; Code:
  34.  
  35. ;;;###autoload
  36. (defun map-y-or-n-p (prompter actor list &optional help action-alist)
  37.   "Ask a series of boolean questions.
  38. Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
  39.  
  40. LIST is a list of objects, or a function of no arguments to return the next
  41. object or nil.
  42.  
  43. If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\).  If not
  44. a string, PROMPTER is a function of one arg (an object from LIST), which
  45. returns a string to be used as the prompt for that object.  If the return
  46. value is not a string, it is eval'd to get the answer; it may be nil to
  47. ignore the object, t to act on the object without asking the user, or a
  48. form to do a more complex prompt.
  49.  
  50. ACTOR is a function of one arg (an object from LIST),
  51. which gets called with each object that the user answers `yes' for.
  52.  
  53. If HELP is given, it is a list (OBJECT OBJECTS ACTION),
  54. where OBJECT is a string giving the singular noun for an elt of LIST;
  55. OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
  56. verb describing ACTOR.  The default is \(\"object\" \"objects\" \"act on\"\).
  57.  
  58. At the prompts, the user may enter y, Y, or SPC to act on that object;
  59. n, N, or DEL to skip that object; ! to act on all following objects;
  60. ESC or q to exit (skip all following objects); . (period) to act on the
  61. current object and then exit; or \\[help-command] to get help.
  62.  
  63. If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys
  64. that will be accepted.  KEY is a character; FUNCTION is a function of one
  65. arg (an object from LIST); HELP is a string.  When the user hits KEY,
  66. FUNCTION is called.  If it returns non-nil, the object is considered
  67. \"acted upon\", and the next object from LIST is processed.  If it returns
  68. nil, the prompt is repeated for the same object.
  69.  
  70. This function uses `query-replace-map' to define the standard responses,
  71. but not all of the responses which `query-replace' understands
  72. are meaningful here.
  73.  
  74. Returns the number of actions taken."
  75.   (let* ((user-keys (if action-alist
  76.             (concat (mapconcat (function
  77.                         (lambda (elt)
  78.                           (key-description
  79.                            (char-to-string (car elt)))))
  80.                        action-alist ", ")
  81.                 " ")
  82.               ""))
  83.      ;; Make a map that defines each user key as a vector containing
  84.      ;; its definition.
  85.      (map (cons 'keymap
  86.             (append (mapcar (lambda (elt)
  87.                       (cons (car elt) (vector (nth 1 elt))))
  88.                     action-alist)
  89.                 query-replace-map)))
  90.      (actions 0)
  91.      prompt char elt tail def delayed-switch-frame
  92.      (next (if (or (symbolp list)
  93.                (subrp list)
  94.                (byte-code-function-p list)
  95.                (and (consp list)
  96.                 (eq (car list) 'lambda)))
  97.            (function (lambda ()
  98.                    (setq elt (funcall list))))
  99.          (function (lambda ()
  100.                  (if list
  101.                  (progn
  102.                    (setq elt (car list)
  103.                      list (cdr list))
  104.                    t)
  105.                    nil))))))
  106.     (unwind-protect
  107.     (progn
  108.       (if (stringp prompter)
  109.           (setq prompter (` (lambda (object)
  110.                   (format (, prompter) object)))))
  111.       (while (funcall next)
  112.         (setq prompt (funcall prompter elt))
  113.         (if (stringp prompt)
  114.         (progn
  115.           (setq quit-flag nil)
  116.           ;; Prompt the user about this object.
  117.           (let ((cursor-in-echo-area t))
  118.             (message "%s(y, n, !, ., q, %sor %s) "
  119.                  prompt user-keys
  120.                  (key-description (char-to-string help-char)))
  121.             (setq char (read-event)))
  122.           ;; Show the answer to the question.
  123.           (message "%s(y, n, !, ., q, %sor %s) %s"
  124.                prompt user-keys
  125.                (key-description (char-to-string help-char))
  126.                (single-key-description char))
  127.           (setq def (lookup-key map (vector char)))
  128.           (cond ((eq def 'exit)
  129.              (setq next (function (lambda () nil))))
  130.             ((eq def 'act)
  131.              ;; Act on the object.
  132.              (funcall actor elt)
  133.              (setq actions (1+ actions)))
  134.             ((eq def 'skip)
  135.              ;; Skip the object.
  136.              )
  137.             ((eq def 'act-and-exit)
  138.              ;; Act on the object and then exit.
  139.              (funcall actor elt)
  140.              (setq actions (1+ actions)
  141.                    next (function (lambda () nil))))
  142.             ((eq def 'quit)
  143.              (setq quit-flag t)
  144.              (setq next (` (lambda ()
  145.                      (setq next '(, next))
  146.                      '(, elt)))))
  147.             ((eq def 'automatic)
  148.              ;; Act on this and all following objects.
  149.              (if (eval (funcall prompter elt))
  150.                  (progn
  151.                    (funcall actor elt)
  152.                    (setq actions (1+ actions))))
  153.              (while (funcall next)
  154.                (if (eval (funcall prompter elt))
  155.                    (progn
  156.                  (funcall actor elt)
  157.                  (setq actions (1+ actions))))))
  158.             ((eq def 'help)
  159.              (with-output-to-temp-buffer "*Help*"
  160.                (princ
  161.                 (let ((object (if help (nth 0 help) "object"))
  162.                   (objects (if help (nth 1 help) "objects"))
  163.                   (action (if help (nth 2 help) "act on")))
  164.                   (concat
  165.                    (format "Type SPC or `y' to %s the current %s;
  166. DEL or `n' to skip the current %s;
  167. ! to %s all remaining %s;
  168. ESC or `q' to exit;\n"
  169.                        action object object action objects)
  170.                       (mapconcat (function
  171.                           (lambda (elt)
  172.                             (format "%c to %s"
  173.                                 (nth 0 elt)
  174.                                 (nth 2 elt))))
  175.                          action-alist
  176.                          ";\n")
  177.                       (if action-alist ";\n")
  178.                       (format "or . (period) to %s \
  179. the current %s and exit."
  180.                           action object)))))
  181.  
  182.              (setq next (` (lambda ()
  183.                      (setq next '(, next))
  184.                      '(, elt)))))
  185.             ((vectorp def)
  186.              ;; A user-defined key.
  187.              (if (funcall (aref def 0) elt) ;Call its function.
  188.                  ;; The function has eaten this object.
  189.                  (setq actions (1+ actions))
  190.                ;; Regurgitated; try again.
  191.                (setq next (` (lambda ()
  192.                        (setq next '(, next))
  193.                        '(, elt))))))
  194.             ((and (consp char)
  195.                   (eq (car char) 'switch-frame))
  196.              ;; switch-frame event.  Put it off until we're done.
  197.              (setq delayed-switch-frame char)
  198.              (setq next (` (lambda ()
  199.                      (setq next '(, next))
  200.                      '(, elt)))))
  201.             (t
  202.              ;; Random char.
  203.              (message "Type %s for help."
  204.                   (key-description (char-to-string help-char)))
  205.              (beep)
  206.              (sit-for 1)
  207.              (setq next (` (lambda ()
  208.                      (setq next '(, next))
  209.                      '(, elt)))))))
  210.           (if (eval prompt)
  211.           (progn
  212.             (funcall actor elt)
  213.             (setq actions (1+ actions)))))))
  214.       (if delayed-switch-frame
  215.       (setq unread-command-events
  216.         (cons delayed-switch-frame unread-command-events))))
  217.     ;; Clear the last prompt from the minibuffer.
  218.     (message "")
  219.     ;; Return the number of actions that were taken.
  220.     actions))
  221.  
  222. ;;; map-ynp.el ends here
  223.